package com.nuo.modules.news.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuo.common.utils.PageUtils;
import com.nuo.common.utils.R;
import com.nuo.common.utils.RichTextUtil;
import com.nuo.modules.news.entity.NewsEntity;
import com.nuo.modules.news.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 *
 *
 * @author nuo
 * @email 3033616351@qq.com
 * @date 2022-11-04 16:30:58
 */
@RestController
@RequestMapping("news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    /*
     前台获取用户列表
     */
    @GetMapping("/fontList")
    public R fontlist(@RequestParam Map<String, Object> params) {
        String category = (String) params.get("category");
        List<NewsEntity> list = newsService.list(new LambdaQueryWrapper<NewsEntity>().eq(NewsEntity::getCategory, category).orderByDesc(NewsEntity::getCreateTime));
        return R.ok().put("list", list);
    }

    /*
     前台获取轮播图
     */
    @GetMapping("/lunBo")
    public R lunBo() {
        return R.ok().put("list", newsService.page(
                        new Page<>(1, 3),
                        new LambdaQueryWrapper<NewsEntity>()
                                .eq(NewsEntity::getCategory, "轮播图")
                                .orderByDesc(NewsEntity::getUpdateTime)
                        ).getRecords()
        );
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = newsService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        NewsEntity news = newsService.getById(id);
        return R.ok().put("news", news);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody NewsEntity news) throws IOException {
        String message = news.getMessage();
        RichTextUtil richTextUtil = new RichTextUtil();
        // 将Base转化成图片
        news.setMessage(richTextUtil.richText(message));
        newsService.save(news);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody NewsEntity news) throws IOException {
        String message = news.getMessage();
        RichTextUtil richTextUtil = new RichTextUtil();
        // 将Base转化成图片
        news.setMessage(richTextUtil.richText(message));
        newsService.updateById(news);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        newsService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


}
